package com.wanmait.hotelManage.webservlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.sql.Timestamp;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.wanmait.hotelManage.dao.OrderDAO;
import com.wanmait.hotelManage.dao.BreakfastDAO;
import com.wanmait.hotelManage.dao.CarDAO;
import com.wanmait.hotelManage.dao.PagerDAO;
import com.wanmait.hotelManage.dao.RoomTypeCommentDAO;
import com.wanmait.hotelManage.dao.RoomTypeCommentReportDAO;
import com.wanmait.hotelManage.dao.RoomTypeDAO;
import com.wanmait.hotelManage.dao.RoomimagesDAO;
import com.wanmait.hotelManage.vo.Breakfast;
import com.wanmait.hotelManage.vo.Car;
import com.wanmait.hotelManage.vo.Pager;
import com.wanmait.hotelManage.vo.Room;
import com.wanmait.hotelManage.vo.RoomType;
import com.wanmait.hotelManage.vo.RoomTypeComment;
import com.wanmait.hotelManage.vo.Roomimages;
import com.wanmait.hotelManage.vo.Userinfo;

/**
 * Servlet implementation class roomListServlet
 */
@WebServlet("/roomListServlet")
public class roomListServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public roomListServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
    RoomType roomType=null;
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException 
	{
		request.setCharacterEncoding("utf-8");
		String action=request.getParameter("action");
		 if("allroomtype".equals(action)) {
			this.allRoomType(request, response);
		}
		else if("getroomtypes".equals(action)) {
			this.getRoomTypes(request, response);
		}
		else if("roomDetail".equals(action)) {
			this.roomDetail(request, response);
			
		}
		else if("order".equals(action)) {
			this.order(request, response);
		}else if("getSearchRoomTypeList".equals(action)) {
			this.getSearchRoomTypeList(request, response);
		}else if("showRoomDetail".equals(action)) {
			this.showRoomDetail(request, response);
		}
		else if("de".equals(action)) {
			this.de(request, response);
		}
		else if("comment".equals(action)) {
			this.comment(request, response);
		}
		
	}
	protected void comment(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException 
	{
		String roomTypeName=request.getParameter("roomTypeName");
		System.out.println(roomTypeName);
		String startTime=request.getParameter("startTime");
		String endTime=request.getParameter("endTime");
	     roomType=	(RoomType)RoomTypeDAO.getRoomtypedao().roomTypeDetalis(roomTypeName);
	    List<RoomType> roomTypes = RoomTypeDAO.getRoomtypedao().findAll(); 
		request.setAttribute("roomTypes", roomTypes);
	     List<Roomimages>images=RoomimagesDAO.getRoomimagesdao().findImagesByRoomTypeId(roomType);
	     ArrayList<RoomTypeComment>roomTypeComments=RoomTypeCommentDAO.getRoomtypecommentdao().getfindall(roomType);
	     int sum=RoomTypeCommentDAO.getRoomtypecommentdao().getsum(roomType);
		request.setAttribute("roomType", roomType);
		request.setAttribute("sum", sum);
		request.setAttribute("roomTypeComments", roomTypeComments);
		request.setAttribute("images", images);
		request.setAttribute("startTime", startTime);
		request.setAttribute("endTime", endTime);
		request.getRequestDispatcher("/FrontDesk/center/comment.jsp").forward(request, response);
	}
	protected void de(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException 
	{
		Integer id=Integer.parseInt(request.getParameter("id"));
		String a=request.getParameter("a");
		HttpSession session=request.getSession();
		Userinfo userinfo=(Userinfo)session.getAttribute("userinfo");
		PrintWriter pw=response.getWriter();
		if(userinfo!=null) {
			RoomTypeCommentReportDAO.getRoomtypecommentreportdao().insert(id,userinfo.getId(),a);
			pw.print("ok");
		}else {
			session.setAttribute("goUrl", request.getHeader("Referer"));
			pw.print("no");
		}
		
	}
	//下拉列表框,展示其他的房型
	protected void showRoomDetail(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException 
	{
		String roomtype = request.getParameter("roomType");
		 roomType=	(RoomType)RoomTypeDAO.getRoomtypedao().roomTypeDetalis(roomtype);
		request.setAttribute("roomType", roomType);
		
		
		
		String startTime=request.getParameter("startTime");
		String endTime=request.getParameter("endTime");
	    List<RoomType> roomTypes = RoomTypeDAO.getRoomtypedao().findAll(); 
		request.setAttribute("roomTypes", roomTypes);
	     List<Roomimages>images=RoomimagesDAO.getRoomimagesdao().findImagesByRoomTypeId(roomType);
	     ArrayList<RoomTypeComment>roomTypeComments=RoomTypeCommentDAO.getRoomtypecommentdao().getfindall(roomType);
	     int sum=RoomTypeCommentDAO.getRoomtypecommentdao().getsum(roomType);
		request.setAttribute("roomType", roomType);
		request.setAttribute("sum", sum);
		request.setAttribute("roomTypeComments", roomTypeComments);
		request.setAttribute("images", images);
		request.setAttribute("startTime", startTime);
		request.setAttribute("endTime", endTime);
//		request.getRequestDispatcher("/FrontDesk/roomDetail.jsp").forward(request, response);
		request.getRequestDispatcher("/FrontDesk/showRoomTypeDetail.jsp").forward(request, response);
	}
	//搜索后分页
	protected void getSearchRoomTypeList(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException 
	{
		String startTime = request.getParameter("startTime");
		String endTime = request.getParameter("endTime");
		String pageNo = request.getParameter("pageNo");
		
		DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
		Date st=null;
		Date et=null;
		try {
			st=dateFormat.parse(startTime);
			et=dateFormat.parse(endTime);
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		Timestamp tst=new Timestamp(st.getTime());
		Timestamp tet=new Timestamp(et.getTime());
		//获得入住的每一天
		List<Timestamp> timestamps=new ArrayList<Timestamp>();
		while(true) {
			if(tst.before(tet)) {
				timestamps.add(tst);
				Calendar startCalendar = Calendar.getInstance();
				startCalendar.setTime(tst);
				startCalendar.add(startCalendar.DATE, 1);
				tst = new Timestamp(startCalendar.getTimeInMillis());
			}else {
				break;
			}
		}

		List<RoomType> allroomTypes=(List<RoomType>)RoomTypeDAO.getRoomtypedao().findAll();
		
		Pager pager=new Pager();
		pager.setPageSize(2);
		pager.setPageNo(Integer.parseInt(pageNo));
		int rows = RoomTypeDAO.getRoomtypedao().getSearchRows(allroomTypes, timestamps);
		pager.setPageCount(rows);
		
		List<RoomType> roomTypes=RoomTypeDAO.getRoomtypedao().search(allroomTypes,timestamps,pager);
		
		request.setAttribute("startTime", startTime);
		request.setAttribute("endTime", endTime);
		request.setAttribute("pager",pager);
		request.setAttribute("roomTypes", roomTypes);
		request.getRequestDispatcher("/FrontDesk/showSearchRoomTypeList.jsp").forward(request, response);

		
	}
	
	protected void order(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException 
	{
	  String erro=request.getParameter("erro");
	  request.setAttribute("roomType", roomType);
		request.getRequestDispatcher("/FrontDesk/roomDetail.jsp?erro="+erro).forward(request, response);
	}
	protected void roomDetail(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException 
	{
		String roomTypeName=request.getParameter("roomTypeName");
		String startTime=request.getParameter("startTime");
		String endTime=request.getParameter("endTime");
	    roomType=	(RoomType)RoomTypeDAO.getRoomtypedao().roomTypeDetalis(roomTypeName);
	    List<RoomType> roomTypes = RoomTypeDAO.getRoomtypedao().findAll(); 
		request.setAttribute("roomTypes", roomTypes);
	    List<Roomimages>images=RoomimagesDAO.getRoomimagesdao().findImagesByRoomTypeId(roomType);
	    ArrayList<RoomTypeComment>roomTypeComments=RoomTypeCommentDAO.getRoomtypecommentdao().getfindall(roomType);
	    int sum=RoomTypeCommentDAO.getRoomtypecommentdao().getsum(roomType);
	    Breakfast breakfast = BreakfastDAO.getBreakfastdao().findall();
	    Car car=CarDAO.getCarDAO().getCar();
	    
	    request.setAttribute("car", car);
	    request.setAttribute("breakfast", breakfast);
		request.setAttribute("roomType", roomType);
		request.setAttribute("sum", sum);
		request.setAttribute("roomTypeComments", roomTypeComments);
		request.setAttribute("images", images);
		request.setAttribute("startTime", startTime);
		request.setAttribute("endTime", endTime);
		request.getRequestDispatcher("/FrontDesk/roomDetail.jsp").forward(request, response);
	}
	protected void getRoomTypes(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException 
	{
		String pageNo=request.getParameter("pageNo");
		Pager pager=new Pager();
		pager.setPageSize(2);
		int rows=PagerDAO.getPagerdao().getrows();
		pager.setPageCount(rows);
		pager.setPageNo(Integer.parseInt(pageNo));
		List<RoomType> roomTypes=RoomTypeDAO.getRoomtypedao().getPageAllRoomType(pager);
        request.setAttribute("roomTypes", roomTypes);
		request.getRequestDispatcher("/FrontDesk/showroomTypesList.jsp").forward(request, response);
		
		
	}
	protected void allRoomType(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException 
	{
		//数字
				Pager pager = new Pager();
				pager.setPageSize(2);//每页显示的记录数
				int rows = RoomTypeDAO.getRoomtypedao().getRows();//总行数
				pager.setPageCount(rows);//总页数
				pager.setPageNo(1);//默认显示第一页
	List<RoomType> roomTypes=RoomTypeDAO.getRoomtypedao().getPageAllRoomType(pager);
	request.setAttribute("pager",pager);
	request.setAttribute("roomTypes", roomTypes);
	request.getRequestDispatcher("/FrontDesk/roomList.jsp").forward(request, response);
		
		
	}
	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
